/*
See LICENSE folder for this sample’s licensing information.

Abstract:
Main entry point for the application.
*/

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
        print("index \(MyFileManager.shared.userDefaultIndex)")
   
//        MyFileManager.shared.userDefaultIndex = 0
        
        if let launch = launchOptions {
            let num = launch[UIApplication.LaunchOptionsKey.location] as! NSNumber
            MyFileManager.shared.sendBluetoothState(state: "didFinishLaunchingWithOptions", content: String(num.intValue))
            Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { (timer) in
                MyFileManager.shared.sendBluetoothState(state: "timer", content:"timer")
                }
        }else{
            MyFileManager.shared.sendBluetoothState(state: "didFinishLaunchingWithOptions", content: "noLaunchOptions")
        }
      
        return true
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        print(#function)
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        print(#function)
        //如果app先进入后台，再被杀死，这个方法就不会被调用
        //在前台直接被杀死，这个方法会被调用，但是下面代码 MyFileManager.shared.userDefaultIndex = 0会执行， MyFileManager.shared.userDefaultIndex! += 1不会执行，说明留给执行代码的时间及短，没有官方文档说的5秒
      
        if MyFileManager.shared.userDefaultIndex == nil {
            MyFileManager.shared.userDefaultIndex = 0
        }else{
            MyFileManager.shared.userDefaultIndex! += 1
        }
        MyFileManager.shared.sendBluetoothState(state: "applicationWillTerminate", content: "")
        
    }
    
    
    
}
